'''
Completeness: 10%

This code goes through the studies reported in Radford et al. and dumps the raw json data for all experimental sessions.
'''

import os, csv, json
import datetime as dt
import cPickle
import pandas as pd
import numpy as np
from scipy.stats import gaussian_kde,ttest_ind
from matplotlib import pyplot as pp
import urllib
from httplib2 import Http

def urlConstruct(params,game):
    tok='???'   #this is a private token which VS researchers have for autmatically downloading data.
    url='http://volunteerscience.com/data/'+game+'/?format=json&'+urllib.urlencode(params)+'&token='+tok
    client=Http()
    content = client.request(url,method='GET',body='')
    data = json.loads(content[1])['tests']
    return data



params=[('format','json'),('variables','all'),('amt','exclude'),('pi','exclude'),('start','01/01/2014'),('end','12/31/2015')]#('end',dt.date.strftime(dt.date.today(),"%m/%d/%Y"))]
studies={'TSP':'3','Flanker':'90','Stroop':'89','JW_Reaction_Time':'173'}

for name,game in studies.iteritems():
    data=urlConstruct(params,game)
    print 'saving ', name
    with open('Dump_Data/'+name+'.json','wb') as f:
        json.dump(data,f)


studies={'Prisoners_Dilemma':'81','Commons_Dilemma':'80'}
params=[('format','json'),('variables','all'),('amt','exclude'),('pi','exclude'),('start','12/09/2015'),('end','12/31/2015')]#('end',dt.date.strftime(dt.date.today(),"%m/%d/%Y"))]
for name,game in studies.iteritems():
    data=urlConstruct(params,game)
    print 'saving ', name
    with open('Raw_Data/'+name+'.json','wb') as f:
        json.dump(data,f)




